//
//  UserTableViewController.m
//  Homework13_DouBan
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 ChieLi. All rights reserved.
//

#import "UserTableViewController.h"
#import "LoginViewController.h"
#import "AVUser.h"
#import "CollectionActivityTableViewController.h"
#import "CollectionMovTableViewController.h"
#import "CollectionDataHandle.h"

@interface UserTableViewController ()

@property (strong, nonatomic) NSArray *titleArray;

@property (assign, nonatomic) BOOL isLogin;

@end

@implementation UserTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    if ([AVUser currentUser]) {
        
        // 当前是否有用户登陆，如果有改为注销
        self.isLogin = YES;
        self.navigationItem.rightBarButtonItem.title = @"注销";
        self.navigationItem.title = [AVUser currentUser].username;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtonAction:)];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.titleArray = @[@"我的活动", @"我的电影", @"清除缓存"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        if ([AVUser currentUser]) {
            
            CollectionActivityTableViewController *vc = [[CollectionActivityTableViewController alloc] init];
            
            [self.navigationController showViewController:vc sender:nil];
        } else {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            
            UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            
            [self presentViewController:loginNC animated:YES completion:^{
                
            }];
        }
        
        
    }
    
    if (indexPath.row == 1) {
        
        if ([AVUser currentUser]) {
            
            CollectionMovTableViewController *vc = [[CollectionMovTableViewController alloc] init];
            
            [self.navigationController showViewController:vc sender:nil];
        } else {
        
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            
            UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            
            [self presentViewController:loginNC animated:YES completion:^{
                
            }];
        }
    }
    
    if (indexPath.row == 2) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"将失去收藏资料" preferredStyle:(UIAlertControllerStyleActionSheet)];
        
        
        UIAlertAction *yes = [UIAlertAction actionWithTitle:@"确认删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
           
            [[CollectionDataHandle sharedCollection] openDB];
            [[CollectionDataHandle sharedCollection] clearAct];
            [[CollectionDataHandle sharedCollection] clearMov];
            [[CollectionDataHandle sharedCollection] closeDB];
            
            
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:yes];
        [alert addAction:cancel];
        
        [self presentViewController:alert animated:yes completion:^{
            
        }];
        
    }
}


#pragma mark - login / logout
- (void)rightButtonAction:(UIBarButtonItem *)sender
{
    if (self.isLogin == NO) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        
        [self presentViewController:loginNC animated:YES completion:^{
            
        }];
    } else {
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
        
        UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"注销用户" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
           
            // 注销用户
            [AVUser logOut];
            // 更改状态
            self.isLogin = NO;
            self.navigationItem.rightBarButtonItem.title = @"登陆";
            self.navigationItem.title = @"我的";
            
            [CollectionDataHandle sharedCollection].DBPath = nil;
            [[CollectionDataHandle sharedCollection].activityArray removeAllObjects];
            [[CollectionDataHandle sharedCollection].movieArray removeAllObjects];
            
        }];
        
        UIAlertAction *canel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
           
            
        }];
        
        [alert addAction:logOut];
        [alert addAction:canel];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];

    }
}

@end
